package com.eportal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.eportal.aop.LogAnnotation;
import com.eportal.base.BaseDAO;
import com.eportal.domain.UserAlbum;
import com.eportal.service.UserAlbumService;
import com.eportal.util.Config;
import com.framework.jqgrid.JqGridPager;

@Service("userAlbumService")
public class UserAlbumServiceImpl  implements UserAlbumService{
	
	private Map<String, Object> result = new HashMap<String, Object>();
	private static Logger log = Logger.getLogger(UserAlbumServiceImpl.class.getName());
	
	@Resource(name="dao")
	private BaseDAO dao;
	 
	public HttpSession getSession() {
        return ServletActionContext.getRequest().getSession();
    }

	@Override
	public UserAlbum get(Long sid){
		UserAlbum UserAlbum = (UserAlbum)dao.getById(UserAlbum.class, sid);
		return UserAlbum;
	}
	
	public UserAlbum getByWhere(String where){
		StringBuffer sb = new StringBuffer("from UserAlbum where ");
		sb.append(where);
		return (UserAlbum) dao.loadObject(sb.toString());
	}
	
	
	@Override
	public void delete(Long id) {
		UserAlbum UserAlbum = (UserAlbum) dao.getById(UserAlbum.class, id);
        dao.delete(UserAlbum);
	}
	@Override
	public void update(UserAlbum UserAlbum) throws Exception {
        dao.update(UserAlbum);
	}
	
	@Override
	public void delete(List<Long> ids) {
		 for (Long id : ids) {
	            delete(id);
	     }
	}


	/**
     * 删除
     */
    @LogAnnotation(operateDescribe="好邻居删除")  
    public Map<String, Object> deleteMethod(String sid){
    	 try {
    		 result.put("objectId", sid);
             delete(Long.parseLong(sid));
             result.put("code", 1);
             result.put("msg", "操作成功");
         } catch (Exception e) {
             log.error("好邻居删除失败", e);
             result.put("code", 0);
             result.put("msg", "操作失败");
         }
    	 return result;
    }

	@Override
	public List<Map<String, Object>> list(JqGridPager jqGridPager) {
		String userAlbumPic = Config.USERALBUMPIC;
		StringBuffer sb = new StringBuffer();
		sb.append("select t.*,TO_CHAR (t.cTime,'yyyy-mm-dd HH24:mi:ss') as cTime1,co.comNum,'");
		sb.append(Config.USERALBUMPIC);
		sb.append("' as userAlbumPic from T04_USER_ALBUM t left join (select c.albumid,count(sid) as comNum from t04_user_albumcomment c group by c.albumid)co on co.albumid=t.sid");
		sb.append(" left join t04_users u on t.userid= u.userid ");
//		String sql = "select t.*,TO_CHAR (t.cTime,'yyyy-mm-dd HH24:mi:ss') as cTime1,co.comNum,'"+userAlbumPic+"' as userAlbumPic from T04_USER_ALBUM t left join (select c.albumid,count(sid) as comNum from t04_user_albumcomment c group by c.albumid)co on co.albumid=t.sid";
		return dao.query(sb.toString(), jqGridPager);
	}

	@LogAnnotation(operateDescribe="好邻居置顶或取消置顶操作")  
	public Map<String, Object> albumOrderTopMethod(String sid, String status) {
    	 result.put("objectId", sid);
         try {
        	 if (StringUtils.isBlank(status)) {
        		 result.put("code", 0);
                 result.put("msg", "无法判断操作行为");
                 return result;
        	 }
        	 UserAlbum entity = get(Long.parseLong(sid));
        	 if ("0".equals(status)) {
        		 if(!canTop(entity.getCommunityId())){
        			 result.put("code", 0);
                     result.put("msg", "置顶失败，一个小区只能置顶两个");
                     return result;
        		 }
        		 entity.setOrderTop(System.currentTimeMillis());
			 }else{
				 entity.setOrderTop(0L);
			 }
        	 update(entity);
             result.put("code", 1);
             result.put("msg", "操作成功");
         } catch (Exception e) {
             log.error("好邻居更新失败", e);
             result.put("code", 0);
             result.put("msg", "操作失败");
         }
         return result;
    }

	private boolean canTop(String communityId) {
		String hql  = "select count(sid) from UserAlbum where orderTop>0 and communityId='"+communityId+"'";
		int count = dao.countQuery(hql);
		if (count<2) {
			return true;
		}
		return false;
	}

//	@Override
//	public List<UsersApply> userApplyList(JqGridPager jqGridPager) {
//		 String sql = "select * from "
//		 		+ "(select a.sid,a.agtcode,a.communityid,a.tel,a.roomno,a.auname,a.attachfile,a.status,a.ausername,a.atime,a.ctime,c.cbname||b.cbname as installunitid "
//		 		+ "from T04_USERS_APPLY a,"
//		 		+ "t032_cinstallunit b ,"
//		 		+ "t031_cbuilding c "
//		 		+ "where a.installunitid=b.sid "
//		 		+ "and b.cbuildingid= c.sid)";
//	     return dao.query(sql, jqGridPager);
//	}



}
